#account视图所有的表单信息
import random

from utils import encrypt
from web.models import *
from django.conf import settings
from django.shortcuts import render
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django_redis import get_redis_connection
# Create your views here.
#ModelForm表单
#优点1:#重写phone字段方法
from web.models import *
from django import forms
from .bootstrap import Bootstrap #引入Bootstrap
#注册From
class RegModelFrom(Bootstrap,forms.ModelForm):
    #重写pwd方法,密文展示
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':"请输入密码"}),
                          label='密码',
                          min_length=8,
                          max_length=64,
                          error_messages={
                              'min_length':'密码长度不能小于8个字符',
                              'max_length':'密码长度不能大于64个字符',
                          })
    # pwds = forms.CharField(widget=forms.PasswordInput(),label='重复密码',
    #                        min_length=8,
    #                        max_length=64,
    #                        error_messages={
    #                            'min_length': '重复密码长度不能小于8个字符',
    #                            'max_length': '重复密码长度不能大于64个字符',
    #                        })
    #重写phone字段方法正则匹配手机号格式,
    phone_number = forms.CharField(label='手机号',validators=[RegexValidator( r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误')])
    class Meta:
        model = UserRole
        fields = ['real_name','user_name','password','age','sex','occupation','phone_number']  #页面展示具体内容

    def clean_name(self):
        user_name = self.cleaned_data['user_name']
        exists  = UserRole.objects.filter(user_name=user_name).exists()
        if exists:
            raise ValidationError('用户名已存在')
        return user_name
    # def clean_pwd(self):
    #     password = self.cleaned_data['password']
    #     #加密&返回
    #     return encrypt.md5(password)
    # def clean_pwds(self):
    #     #判断两次密码是否一致
    #     password = self.cleaned_data.get('password')
    #     pwds = encrypt.md5(self.cleaned_data['pwds'])
    #     if password !=pwds:
    #         raise ValidationError('两次密码不一致')
    #     return pwds
    def clean_phone(self):
        phone = self.cleaned_data['phone']
        exists  = UserRole.objects.filter(phone=phone).first()
        if exists:
            raise ValidationError('手机号已注册')
        return phone

class LoginForm(Bootstrap,forms.Form):
    user_name = forms.CharField(label='帐号')
    password = forms.CharField(label='密码',widget=forms.PasswordInput(render_value=True))
    # code = forms.CharField(label='图片验证码')
    def __init__(self,request,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.request = request

    def clean_pwd(self):
        pwd = self.cleaned_data['password']
        #加密&返回
        return encrypt.md5(pwd)

    # def clean_code(self):
    #     '''钩子 验证码是否正确'''
    #     code  = self.cleaned_data['code']
    #
    #     #去session获取自己的验证码
    #     session_code = self.request.session.get('image_code')
    #     if not session_code:
    #         raise ValidationError('验证码已过期,请重新输入')
    #
    #     if code.strip().upper() != session_code.strip().upper():
    #         raise ValidationError('验证码错误,请重新输入')
    #     return code


